#include "bsp_lcd.h"

void  Lcd_WriteIndex2(uint8_t comm)
{
    TFT_LCD_SCL_LOW();
    TFT_LCD_CS_LOW();
    //lcd_spi_delay();
    TFT_LCD_SDA_LOW();
    //lcd_spi_delay();
    TFT_LCD_SCL_HIGH();
    //lcd_spi_delay();

    for(uint8_t i = 0;i < 8;i++)
    {
        TFT_LCD_SCL_LOW();
        //lcd_spi_delay();
        if(comm & 0x80)
        {
            TFT_LCD_SDA_HIGH();
        }
        else
        {
            TFT_LCD_SDA_LOW();
        }
        //lcd_spi_delay();
        TFT_LCD_SCL_HIGH();
        //lcd_spi_delay();
        comm<<=1;
    }
    TFT_LCD_CS_HIGH();
}

void  Lcd_WriteData2(uint8_t Data)
{
    TFT_LCD_SCL_LOW();
    TFT_LCD_CS_LOW();
    //lcd_spi_delay();
    TFT_LCD_SDA_HIGH();
    //lcd_spi_delay();
    TFT_LCD_SCL_HIGH();
    //lcd_spi_delay();

    for(uint8_t i = 0;i < 8;i++)
    {
        TFT_LCD_SCL_LOW();
        //lcd_spi_delay();
        if(Data & 0x80)
        {
            TFT_LCD_SDA_HIGH();
        }
        else
        {
            TFT_LCD_SDA_LOW();
        }
        //lcd_spi_delay();
        TFT_LCD_SCL_HIGH();
        //lcd_spi_delay();
        Data<<=1;
    }
    TFT_LCD_CS_HIGH();
}

void Lcd_SetRegion(unsigned int x_start,unsigned int y_start,unsigned int x_end,unsigned int y_end)
{

    Lcd_WriteIndex2(0x2a);
    Lcd_WriteData2(0x00);
    Lcd_WriteData2(x_start);
    Lcd_WriteData2(0x00);
    Lcd_WriteData2(x_end);

    Lcd_WriteIndex2(0x2b);
    Lcd_WriteData2(0x00);
    Lcd_WriteData2(y_start);
    Lcd_WriteData2((y_end >> 8) & 0xFF);
    Lcd_WriteData2(y_end & 0xFF);
    Lcd_WriteIndex2(0x2c);
}

void  SPI_WriteData2(uint8_t Data)
{
    TFT_LCD_SDA_HIGH();
    //lcd_spi_delay();
    TFT_LCD_SCL_HIGH();
    //lcd_spi_delay();
    uint8_t i = 0;
    for(uint8_t i = 0;i < 8;i++)
    {
        TFT_LCD_SCL_LOW();
        //lcd_spi_delay();
        if(Data & 0x80)
            TFT_LCD_SDA_HIGH();
        else 
            TFT_LCD_SDA_LOW();
        //lcd_spi_delay();
        TFT_LCD_SCL_HIGH();
        Data<<=1;
        //lcd_spi_delay();
    }
}

void  LCD_WriteData_16Bit(unsigned int Data)
{
    TFT_LCD_SCL_LOW();
    TFT_LCD_CS_LOW();
    SPI_WriteData2(Data>>8);        // high 8 bits
    //lcd_spi_delay();
    TFT_LCD_CS_HIGH();
    //lcd_spi_delay();
    TFT_LCD_SCL_LOW();
    TFT_LCD_CS_LOW();
    SPI_WriteData2(Data);           // low 8 bits
    TFT_LCD_CS_HIGH();

}